/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.core.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.core.dao.BillsDAO;
import com.core.database.Manager;
import com.core.entities.Bills;
import com.core.exception.DAOException;

/**
 * 
 * @author himanasser
 */
public class BillsDAOImpl implements BillsDAO {
		Bills bill = new Bills();
		ArrayList<Bills> billsList = new ArrayList<Bills>();

	Connection connection = null;
	private static BillsDAOImpl instance=null;
	public static BillsDAOImpl getInstance(){
		if(instance==null){
			instance=new BillsDAOImpl();
		}
		return instance;
	}

	@Override
	public Bills findByName(String name) throws DAOException {
		try {
			connection = Manager.getConnection();
			String sql = "SELECT *FROM bills WHERE name = '" + name + "'";
			PreparedStatement ps = connection.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			int count = 0;
			while (rs.next()) {
				count++;
				if (count == 1) {
					bill.setId(rs.getInt(1));
					bill.setName(rs.getString(2));
					bill.setSpecefication_id(rs.getInt(3));

				}
			}

		} catch (SQLException s) {
			s.printStackTrace();
		}
		return bill;
	}

	@Override
	public List<Bills> findBySpecefication_id(int specefication_id)
			throws DAOException {

		
		try {
			Bills bill = new Bills();
			connection = Manager.getConnection();
			String sql = "SELECT * FROM bills WHERE specefication_id = '"
					+ specefication_id + "'";
			PreparedStatement ps = connection.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				bill.setId(rs.getInt(1));
				bill.setName(rs.getString(2));
				bill.setSpecefication_id(rs.getInt(3));
				billsList.add(bill);

			}
		} catch (SQLException s) {
			s.printStackTrace();
		}
		return billsList;
	}

	@Override
	public void insert(Bills o) throws DAOException {
		try {
			connection = Manager.getConnection();
			String sql = "INSERT INTO bills (name, specefication_id) VALUE (?, ? )";
			PreparedStatement ps = connection.prepareStatement(sql);
			ps.setString(1, o.getName());
			ps.setInt(2, o.getSpecefication_id());

			int result = ps.executeUpdate();
			if (result != 1) {
				throw new SQLException("No row inserted!");
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			throw new RuntimeException("Cannot add new Bill", ex);
		} finally {
			Manager.closeConnection(connection);
		}
	}

	@Override
	public Bills findById(int id) throws DAOException {
		try {
			connection = Manager.getConnection();
			String sql = "SELECT *FROM bills WHERE id = '" + id + "'";
			PreparedStatement ps = connection.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			int count = 0;
			while (rs.next()) {
				count++;
				if (count == 1) {
					bill.setId(rs.getInt(1));
					bill.setName(rs.getString(2));
					bill.setSpecefication_id(rs.getInt(3));

				}
			}

		} catch (SQLException s) {
			s.printStackTrace();
		}
		return bill;

	}

	@Override
	public List<Bills> findAll() throws DAOException {
		try {
			connection = Manager.getConnection();
			String sql = " SELECT * FROM bills ";
			PreparedStatement ps = connection.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				bill.setId(rs.getInt(1));
				bill.setName(rs.getString(2));
				bill.setSpecefication_id(rs.getInt(3));
				billsList.add(bill);

			}
		} catch (SQLException s) {
			s.printStackTrace();
		}
		return billsList;
	}

	@Override
	public void delete(int id) throws DAOException {
		try {
			connection = Manager.getConnection();
			String sql = " DELETE FROM bills WHERE id = '" + id + "'";
			PreparedStatement ps = connection.prepareStatement(sql);
			int result = ps.executeUpdate();
			if (result != 1) {
				ps.setString(1, bill.getName());
				ps.setInt(2, bill.getSpecefication_id());
			}

		} catch (SQLException s) {
			s.printStackTrace();

		}
	}

	@Override
	public void update(Bills o) throws DAOException {

		try {
			connection = Manager.getConnection();
			String sql = "UPDATE bills SET name = '" + o.getName()
					+ " ', specefication_id = '" + o.getSpecefication_id()
					+ "' WHERE id = " + o.getId();
			PreparedStatement ps = connection.prepareStatement(sql);
			int result = ps.executeUpdate(sql);

			if (result != 1) {
				ps.setString(1, o.getName());
				ps.setInt(2, o.getSpecefication_id());
			}

		} catch (SQLException s) {
			s.printStackTrace();
		}
	}
}
